package com.android.lovely.ui.activity

import android.icu.text.DecimalFormat
import android.os.Bundle
import android.view.ViewGroup
import android.widget.ImageView
import androidx.activity.OnBackPressedCallback
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.android.lovely.R
import com.android.lovely.aav.lifecycle.LovelyLifecycleManager
import com.android.lovely.api.service.userService
import com.android.lovely.databinding.ActivityLifecycleBinding
import com.android.lovely.databinding.ItemLifecycleBinding
import com.android.lovely.framework.BaseActivity
import com.android.lovely.framework.BaseAdapter
import com.android.lovely.framework.launchWithLoading
import com.android.lovely.manager.KVStore
import com.android.lovely.ui.activity.LifecycleActivity.BannerAdapter.BannerViewHolder
import com.android.lovely.user.UserManager
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.view.ViewExt.addStatusBarMarginTop
import com.kotlin.extensions.view.ViewExt.clickable
import io.glassfy.androidsdk.GooglePlayManager
import io.glassfy.androidsdk.model.PurchaseParams
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive

class LifecycleActivity : BaseActivity<ActivityLifecycleBinding>() {

    private val adapter by lazy { LifecycleAdapter() }

    override fun initView(savedInstanceState: Bundle?) {
        KVStore.virtualShowTime = System.currentTimeMillis()
        mBinding.close.clickable { onBackPressedDispatcher.onBackPressed() }
        mBinding.close.addStatusBarMarginTop()
        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                setResult(RESULT_OK)
                finish()
            }
        })
        if (LovelyLifecycleManager.isEnable().not()) {
            setResult(RESULT_OK)
            finish()
            return
        }
        mBinding.image.adapter = BannerAdapter()
        mBinding.image.isUserInputEnabled = false
        mBinding.image.offscreenPageLimit = 1
        lifecycleScope.launchSafety {
            while (isActive) {
                delay(3000)
                mBinding.image.currentItem++
            }
        }
        val lifecycleInfo = LovelyLifecycleManager.lifecycleInfo ?: return
        adapter.updateList(LovelyLifecycleManager.lovelyLifecycle.map { it.content })
        mBinding.list.adapter = adapter
        mBinding.price.text = getString(R.string.lifecycle_only, DecimalFormat("0.##").format(lifecycleInfo.price))
        mBinding.get.clickable {
            lifecycleScope.launchWithLoading {
                userService.reportVip()
                if (GooglePlayManager.purchase(this@LifecycleActivity, lifecycleInfo.vipProductId, PurchaseParams(UserManager.authId.toString(), lifecycleInfo.price.toFloat()))) {
                    finish()
                }
            }
        }
    }

    private inner class LifecycleAdapter : BaseAdapter<String, ItemLifecycleBinding>() {
        override fun bindViewWithHolderDetails(
            holder: BaseAdapter<String, ItemLifecycleBinding>.ViewHolder,
            item: String?,
            binding: ItemLifecycleBinding,
            position: Int
        ) {
            binding.text.text = item
        }

    }

    private inner class BannerAdapter : RecyclerView.Adapter<BannerViewHolder>() {

        private val images by lazy { listOf(R.drawable.lifecycle_header1, R.drawable.lifecycle_header2, R.drawable.lifecycle_header3, R.drawable.lifecycle_header4) }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
            return BannerViewHolder(ImageView(this@LifecycleActivity).apply {
                layoutParams = RecyclerView.LayoutParams(-1, -1)
            })
        }

        override fun onBindViewHolder(holder: BannerViewHolder, position: Int) {
            val image = images[position % images.size]
            holder.imageView.setImageResource(image)
        }

        override fun getItemCount(): Int {
            return Int.MAX_VALUE
        }

        inner class BannerViewHolder(val imageView: ImageView) : RecyclerView.ViewHolder(imageView)

    }
}